package com.lzy.cloud.account.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.account.dto.AccountWalletDto;
import com.lzy.cloud.account.dto.AccountWalletInfoDto;
import com.lzy.cloud.account.facade.AccountWalletFacade;
import com.lzy.cloud.account.request.AccountWalletInfoRequest;
import com.lzy.cloud.account.request.AccountWalletRequest;
import com.lzy.cloud.account.response.AccountWalletResponse;
import com.lzy.cloud.account.service.AccountWalletInfoService;
import com.lzy.cloud.account.service.AccountWalletService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 账号钱包
 */
@RestController
@RequestMapping(value = "/account")
public class AccountWalletController {

    @Autowired
    private AccountWalletService accountWalletService;
    @Autowired
    private AccountWalletInfoService accountWalletInfoService;
    @Autowired
    private AccountWalletFacade accountWalletFacade;

    /**
     * 获取列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result selectList(AccountWalletRequest request) {
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<AccountWalletDto> accountWalletDtoList = accountWalletService.queryAccountWalletList(pager,DozerUtils.map(request,AccountWalletDto.class));
        Ensure.that(accountWalletDtoList).isNotNull("00010001");
        return new Result(pager,DozerUtils.maps(accountWalletDtoList.getList(), AccountWalletResponse.class));
    }

    /**
     * 修改钱包金额
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result updateById(@RequestBody AccountWalletRequest request){
        accountWalletInfoService.update(DozerUtils.map(request,AccountWalletDto.class));
        return new Result();
    }

    /**
     * 删除金额
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public Result deleteById(@PathVariable Long id){
        accountWalletInfoService.deleteById(id);
        return new Result();
    }

    /**
     * 新增钱包主表
     * @param request
     * @return
     */
    @RequestMapping(value = "/add/wallet",method = RequestMethod.POST)
    public  Result addWallet( AccountWalletRequest request){
        Long id = accountWalletService.insert(DozerUtils.map(request,AccountWalletDto.class));
        return new Result(id);
    }

    /**
     * 新增钱包金额
     * @param request
     * @return
     */
    @RequestMapping(value = "/add/wallet/info",method = RequestMethod.POST)
    public  Result addWalletInfo(@RequestBody AccountWalletInfoRequest request){
        Long id = accountWalletInfoService.add(DozerUtils.map(request,AccountWalletInfoDto.class));
        Ensure.that(id > 0).isTrue("00010003");
        return new Result(id);
    }

    /**
     * 根据订单金额获取该会员是否具有输入支付密码权限
     * @param memberId
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/is/pay",method = RequestMethod.POST)
    public Result isPay(Long memberId,String orderId,Long shopId){
        Result result = new Result();
        Boolean ok = accountWalletFacade.isPay(memberId,orderId,shopId);
        if(ok){
            result.setResult(true);
            return result;
        }else{
            result.setDesc("");
            result.setResult(true);
            return result;
        }
    }

    /**
     * 新增钱包
     * @param accountWalletRequest
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody AccountWalletRequest accountWalletRequest) {
        accountWalletFacade.add(DozerUtils.map(accountWalletRequest, AccountWalletDto.class));
        return new Result();
    }

    /**
     * 根据会员id获取该钱包id
     * @param memberId
     * @return
     */
    @RequestMapping(value = "/find/by/memberid/and/platformid",method = RequestMethod.GET)
    public Result findByMemberIdAndPlatformId(@RequestParam("memberId") Long memberId, @RequestParam("platformId") Long platformId){
        Ensure.that(memberId).isNotNull("00000002");
        Ensure.that(platformId).isNotNull("00000002");
        AccountWalletDto accountWalletDto = accountWalletService.findByMemberIdAndPlatformId(memberId,platformId);
        return new Result(accountWalletDto);
    }

    /**
     * 根据钱包id获取冻结金额
     * @param walletId
     * @return
     */
    @RequestMapping(value = "/find/by/wallet/id/{walletId}",method = RequestMethod.GET)
    public Result findByWalletId(@PathVariable("walletId") Long walletId){
        Ensure.that(walletId).isNotNull("00000002");
        AccountWalletInfoDto accountWalletInfoDto = accountWalletInfoService.selectBlockAccount(walletId);
        return new Result(accountWalletInfoDto);
    }

    /**
     * 更新钱包冻结金额
     * @param accountWalletInfoRequest
     * @return
     */
    @RequestMapping(value = "/update/blocked/wallet",method = RequestMethod.POST)
    public Result updateBlockedWallet(@RequestBody AccountWalletInfoRequest accountWalletInfoRequest){
        Ensure.that(accountWalletInfoRequest).isNotNull("00000002");
        Boolean ok = accountWalletInfoService.updateAccountWalletInfo(DozerUtils.map(accountWalletInfoRequest,AccountWalletInfoDto.class));
        return new Result(ok);
    }

    /**
     * 余额变成冻结金额
     * @param accountWalletDto
     * @return
     */
    @RequestMapping(value = "/balance/become/frozen/amount",method = RequestMethod.POST)
    public Result balanceBecomeFrozenAmount(@RequestBody AccountWalletDto accountWalletDto){
        Ensure.that(accountWalletDto).isNotNull("00000002");
        return new Result(accountWalletFacade.balanceBecomeFrozenAmount(accountWalletDto));
    }

    /**
     * 冻结金额变成余额
     * @param accountWalletDto
     * @return
     */
    @RequestMapping(value = "/frozen/amount/become/balance",method = RequestMethod.POST)
    public Result frozenAmountBecomeBalance(@RequestBody AccountWalletDto accountWalletDto){
        Ensure.that(accountWalletDto).isNotNull("00000002");
        return new Result(accountWalletFacade.frozenAmountBecomeBalance(accountWalletDto));
    }

    /**
     * 根据钱包id获取可用余额
     * @param walletId
     * @return
     */
    @RequestMapping(value = "/select/available/by/wallet/id/{walletId}",method = RequestMethod.GET)
    public Result selectAvailableAccount(@PathVariable("walletId") Long walletId){
        AccountWalletInfoDto walletInfoDto = accountWalletInfoService.selectAvailableAccount(walletId);
        return new Result(walletInfoDto);
    }

    /**
     * 更新钱包可用余额
     * @param accountWalletInfoDto
     * @return
     */
    @RequestMapping(value = "/update/by/primary/key",method = RequestMethod.POST)
    public Result updateByPrimaryKey(AccountWalletInfoDto accountWalletInfoDto){
        Boolean ok = accountWalletInfoService.updateAccountWalletInfo(accountWalletInfoDto);
        return new Result(ok);
    }
}
